import { Ref, computed } from "vue";
// import CommonHook from "@/hook/commonHook";
import { message, message as Message } from "ant-design-vue";
import { getListApi, getPageStaticApi, getShipmentReleaseExcelListApi, passApplyApi } from "@/api/waybillManagement/financeWaybill/releaseCargo";
export default function OperationHook(state: {
  searchForm: ReleaseCargo.SearchForm;
  tableData: Ref<Array<ReleaseCargo.ListRows>>;
  drawerOpts: Base.DrawerOpts;
  dialogOpts: Base.DialogOpts;
  loading: Ref<boolean>;
  tableRef: any;
  pageStat: Ref<Base.GeneraDataType>;
  activeKey: Ref<string>;
}) {
  const { searchForm, tableData, loading, tableRef, dialogOpts, pageStat, activeKey } = state;
  const selectRows = computed<Array<DetainGoods.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  // const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params = {
      ...getParams(),
      statusList: [activeKey.value]
    };
    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        const { records } = data;
        if (code === 200) {
          // tableData.value =
          //   records.map((t: any) => {
          //     t.bizFileDetailReqDtoList = t.bizFileDetailReqDtoList.map((q: any) => {
          //       return { ...q, isVisible: false };
          //     });
          //     return t;
          //   }) || [];
          tableData.value = records || [];
          searchForm.total = Number(data.total);
          searchForm.beforePageNum = searchForm.pageNum;
          console.log(tableData.value);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi({ ...params, statusList: ["1", "2", "3"] }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          1: data[1],
          2: data[2],
          3: data[3]
        });
        // activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2", "3"]));
      }
    });
  };
  const getParams = () => {
    const { applyNoList, bizIdList, beforePageNum, customerIdList, companyIdList, followIdList, pageNum, pageSize } = searchForm;
    const params: any = {
      applyNoList: [],
      companyIdList,
      customerIdList,
      bizIdList,
      followIdList,

      pageNum,
      pageSize,
      beforePageNum,
      statusList: []
    };
    if (applyNoList)
      params.applyNoList = applyNoList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 下载附件
  const downloadFileHandle = (row: Base.GeneraDataType) => {
    window.open(row.url);
  };
  // 驳回
  const rejectReleaseCargoHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `驳回`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "RejectReleaseCargo";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };
  // 通过审批
  const releaseCargoHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    passApplyApi(selectRows.value.map((t: any) => t.id)).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("审批成功!");
        getData();
        pageStateic();
      }
    });
  };

  //批量导出
  const exportHandle = () => {
    const params = {
      ...getParams(),
      idList: selectRows.value.length ? selectRows.value.map((t: any) => t.id) : undefined,
      statusList: [Number(activeKey.value)]
    };
    return getShipmentReleaseExcelListApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!请前往导出中心导出");
        getData();
      }
    });
  };
  return {
    searchHandle,
    getData,
    changeTabHandle,
    rejectReleaseCargoHandle,
    downloadFileHandle,
    releaseCargoHandle,
    pageStateic,
    exportHandle
  };
}
